//-----------------------------------------------------------------------
// <copyright file="GetIPByHostName.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Martin peck</author>
// <email>mpeck</email>
// <date>2006-02-21</date>
// <summary>Returns the IP addresses for a given hostname</summary>
//-----------------------------------------------------------------------
namespace Microsoft.Sdc.Tasks.Network
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Globalization;
    using System.Net;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Returns the IP addresses of a given hostname
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[ 
    /// <Network.GetIPByHostName HostName="www.microsoft.com">
    ///        <Output TaskParameter="FirstIPAddress" PropertyName="firstIP" />
    ///        <Output TaskParameter="AllIPAddress" PropertyName="allIPs" />
    /// </Network.GetIPByName>
    /// ]]></code>
    /// 
    /// <para>where:</para>
    /// <para><i>HostName (Required)</i></para>
    /// <para>hostname to resolve
    /// </para>
    /// <para>FirstIPAddress</para>
    /// <para> The first IP address for the hostname returned by DNS
    /// </para>   
    /// </remarks>
    /// <example>
    /// <code><![CDATA[ 
    /// <Network.Output HostName="www.microsoft.com">
    ///        <Output TaskParameter="FirstIPAddress" PropertyName="firstIP" />
    ///        <Output TaskParameter="AllIPAddress" PropertyName="allIPs" />
    /// </Network.GetIPByName>
    /// ]]></code>    
    /// </example>
    public class GetIPByHostName : TaskBase
    {
        /// <summary>
        /// first IP address
        /// </summary>
        private string firstIPAddress;

        /// <summary>
        /// all IP addresses
        /// </summary>
        private string[] allIPAddresses;

        /// <summary>
        /// host name
        /// </summary>
        private string hostName;

        /// <summary>
        /// Host exists
        /// </summary>
        private bool hostExists;

        /// <summary>
        /// Treat Errors As Warnings
        /// </summary>
        private bool treatErrorsAsWarnings;

        /// <summary>
        /// Initializes a new instance of the GetIPByName class.
        /// </summary>
        public GetIPByHostName()
        {
        }

        /// <summary>
        /// The host name to resolve
        /// </summary>
        [Required]
        public string HostName
        {
            get { return this.hostName; }
            set { this.hostName = value; }
        }

        /// <summary>
        /// Treat Errors As Warnings
        /// </summary>
        public bool TreatErrorsAsWarnings
        {
            get { return this.treatErrorsAsWarnings; }
            set { this.treatErrorsAsWarnings = value; }
        }

        /// <summary>
        /// The IP address
        /// </summary>
        [Output]
        public string IPAddress
        {
            get { return this.firstIPAddress; }
        }   

        /// <summary>
        /// Returns an array of all ip addresses
        /// </summary>
        [Output]
        public string[] AllIPAddresses
        {
            get { return this.allIPAddresses; }
        }

        /// <summary>
        /// HostExists
        /// </summary>
        [Output]
        public bool HostExists
        {
            get { return this.hostExists; }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            try
            {
                IPHostEntry hostEntry = Dns.GetHostEntry(this.hostName);

                this.firstIPAddress = hostEntry.AddressList[0].ToString();

                List<string> addresses = new List<string>();

                foreach (IPAddress address in hostEntry.AddressList)
                {
                    addresses.Add(address.ToString());
                }

                this.allIPAddresses = addresses.ToArray();

                this.hostExists = true;
            }
            catch (System.Exception ex)
            {
                this.hostExists = false;
                this.LogError(ex);
            }
        }

        private void LogError(Exception e)
        {
            if (this.treatErrorsAsWarnings)
            {
                base.Log.LogWarningWithCodeFromResources("Network.GetIPFromHostName.NotFound", this.hostName, e.Message);
            }
            else
            {
                base.Log.LogErrorWithCodeFromResources("Network.GetIPFromHostName.NotFound", this.hostName, e.Message);                
            }
        }
    }
}
